import UserModule from './user-module';
import Tools from './tools';
let UserInfo = {};
UserInfo.setUserInfo = function(userInfo) {
    let array = [];
    let secret = 'manage';
    let data = {
        UserID: userInfo.UserID,
        UserRealName: userInfo.UserRealName,
        UserRule: userInfo.UserRule,
        UserPartnersID:userInfo.UserPartnersID,
        UserOrgStructID: userInfo.UserOrgStructID,
        UserPartnersSign:userInfo.UserPartnersSign,
        UserAccessToken:userInfo.UserAccessToken,
        AuthToken:userInfo.AuthToken,
        RuleDataDegreeCode:userInfo.RuleDataDegreeCode,
        IsShowAll:userInfo.IsShowAll
    };
    for (let i in data) {
        if(data[i]){
            array.push(data[i]);
        }

    }
    array.push(secret);
    array.sort();
    console.log(array);
    let all_value_str = array.join();

    let vk = Tools.md5.hex_md5(all_value_str);
    data.VK = vk;
    sessionStorage.setItem('user-info', JSON.stringify(data));
}



UserInfo.getUserInfo = function() {
    let array = [];
    let secret = 'manage';

    let userInfoStr = sessionStorage.getItem('user-info');

    if (userInfoStr && userInfoStr !== '') {
        let obj = JSON.parse(userInfoStr);
        //console.log(obj);
        for (let i in obj) {
            if (i !== 'VK') {
                if(obj[i]){
                    array.push(obj[i]);
                }
            }
        }
        array.push(secret);

        array.sort();
       // console.log(array);
        let all_value_str = array.join();

        let vk = Tools.md5.hex_md5(all_value_str);
        console.log(vk,obj.VK);
        if (obj.VK === vk) {
            return obj;
        }
    }
    return null;
}

UserInfo.clear = function() {
    sessionStorage.removeItem("user-info");
    sessionStorage.removeItem("user-module");
}

UserInfo.UserAccessToken=function(){
    let info = UserInfo.getUserInfo();
    if (info) {
        return info.UserAccessToken;
    }
    return '';
}

UserInfo.AuthToken=function(){
    let info = UserInfo.getUserInfo();
    if (info) {
        return info.AuthToken;
    }
    return '';
}





UserInfo.UserID = function() {
    let info = UserInfo.getUserInfo();
    if (info) {
        return info.UserID;
    }
    return 0;
}

UserInfo.RealName = function() {
    let info = UserInfo.getUserInfo();
    if (info) {
        return info.RealName;
    }
    return '';
}

UserInfo.UserRule = function() {
    let info = UserInfo.getUserInfo();
    if (info) {
        return info.UserRule;
    }
    return '';
}

UserInfo.UserOrgStructID = function() {
    let info = UserInfo.getUserInfo();
    if (info) {
        return info.UserOrgStructID;
    }
    return '';
}

UserInfo.UserPartnersID = function() {
    let info = UserInfo.getUserInfo();
    if (info) {
        return info.UserPartnersID;
    }
    return 0;
}

UserInfo.UserPartnersSign=function(){
    let info = UserInfo.getUserInfo();
    if (info) {
        return info.UserPartnersSign;
    }
    return '';
}

UserInfo.setUserModule = function(modules) {
    UserModule.Set(modules);
}

UserInfo.getUserModule = function(cb) {
    return UserModule.Get();
}

UserInfo.getUserRuleAble = function(cb) {
    let userModuleItems = UserInfo.getUserModule();
    let _RuleAbleList = [];
    for (let i in userModuleItems) {
        let RA = {
            M: ret[i].ModuleID,
            A: ret[i].Able
        };
        _RuleAbleList.push(RA);
    }
    return _RuleAbleList();
}

export default UserInfo;
